import allure

from handlers.handler_mysql import HandlerMysql
from handlers.handler_log import logger


def mysql_assertion(key, conn: HandlerMysql, sql: str) -> int:
    result = 0
    try:
        data = conn.query(sql)
        assert data is not None, f"sql的{key}断言失败！，sql语句'{sql}'执行failed"
        logger.success(f"sql的{key}断言成功！，sql语句'{sql}'执行successfully")
    except AssertionError as e:
        logger.error(e)
        result = 1
        allure.attach(name="mysql断言错误内容", body=f'1111111{str(e)}', attachment_type=allure.attachment_type.TEXT)
    finally:
        return result
